package com.share.service.impl;

import com.share.common.enums.AccountTypeEnum;
import com.share.constant.OAuthConstant;
import com.share.factory.AuthStrategyFactory;
import com.share.factory.strategy.AbstractAuthStategy;
import com.share.factory.strategy.AuthStrategy;
import com.share.properties.GiteeProperties;
import com.share.properties.WeChatProperties;
import com.share.service.OAuthService;
import com.share.service.WebSocketService;
import com.share.user.Gitee.GiteeUserInfo;
import com.share.user.Gitee.ProviderToken;
import com.share.user.weChat.dtos.TokenInfo;
import com.share.user.weChat.dtos.WxUserInfo;
import com.share.util.common.HttpClientUtils;
import com.share.utils.TokenUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class OAuthServiceImpl implements OAuthService {

    private final GiteeProperties giteeProperties;
    private final WeChatProperties weChatProperties;
    private final WebSocketService webSocketService;
    private final TokenUtils tokenUtils;
    private final AuthStrategyFactory authStrategyFactory;

    @Autowired
    public OAuthServiceImpl(GiteeProperties giteeProperties, WeChatProperties weChatProperties, WebSocketService webSocketService, TokenUtils tokenUtils, AuthStrategyFactory authStrategyFactory) {
        this.giteeProperties = giteeProperties;
        this.weChatProperties = weChatProperties;
        this.webSocketService = webSocketService;
        this.tokenUtils = tokenUtils;
        this.authStrategyFactory = authStrategyFactory;
    }


    @Override
    public String getAuthorizationUrl(AccountTypeEnum authType, Map<String, String> params) {
        return "";
    }

    @Override
    public String getAccessToken(AccountTypeEnum authType, String code, Map<String, String> params) {
        return "";
    }

    @Override
    public String thirdPartyLogin(AccountTypeEnum authType, String code) {
        AbstractAuthStategy authStrategy = authStrategyFactory.getAuthStrategy(authType);
        authStrategy.thirdPartyLogin(code);
        return "index";
    }

    @Override
    public String refreshAccessToken(AccountTypeEnum authType, String refreshToken, Map<String, String> params) {
        return "";
    }



    // 微信获取Token
    @SneakyThrows
    public TokenInfo getWxTokenInfo(String code, String appID, String appSecret){
        String url = String.format(OAuthConstant.WX_TOKEN_URL, appID, appSecret, code);
        return HttpClientUtils.executeGetRequest(url, TokenInfo.class,null);
    }

    // 微信获取用户信息
    @SneakyThrows
    public WxUserInfo getWxUserInfo(TokenInfo tokenInfo){
        String url = String.format(OAuthConstant.WX_USER_INFO_URL, tokenInfo.getAccessToken(), tokenInfo.getOpenId());
        return HttpClientUtils.executeGetRequest(url, WxUserInfo.class,null);
    }

    // Gitee获取Token
    @SneakyThrows
    public Map getGiteeToken(ProviderToken providerToken){
        String url = String.format(OAuthConstant.GITEE_TOKEN_URL, providerToken.getCode(),
                providerToken.getClientId(),
                providerToken.getRedirectUri(),
                providerToken.getClientSecret());
        return HttpClientUtils.executeGetRequest(url, Map.class,providerToken);
    }

    // Gitee获取用户信息
    @SneakyThrows
    public GiteeUserInfo getGiteeUser(String accessToken) {
        String url = String.format(OAuthConstant.GITEE_USER_INFO_URL, accessToken);
        return HttpClientUtils.executeGetRequest(url, GiteeUserInfo.class,null);
    }



}
